<?php
/*
 * 商品确认收货
 */
namespace Mobile\Controller;

use Mobile\Model\orderModel;
use Mobile\Model\moneylogModel;

class OrdershouhuoController extends BaseController {

    public $orderModel;
    public $moneylogModel;
    
    
    public function __construct() {
        parent::__construct();
        $this->orderModel = new orderModel();
        $this->moneylogModel = new moneylogModel();
    }
    
    /*
     * 确认收货ajax
     */
    public function index(){
        $order_id = get("order_id");
        //判断这个订单是不是我的
        $where = array(
            "order_id" => $order_id,
            "uid" => $this->uid
        );
        $orderinfo = $this->orderModel->getOrder($where);
        if(empty($orderinfo)){
            return_json(1);
        }
    
        $res = $this->_shouhuo($orderinfo);
    
        //TODO 发送收货通知
        if($res){
            return_json(0,"已确认收货","","/mobile/orderdetail?order_id=".$order_id);
        }
    
        return_json(1,"当前状态不能确认收货");
    }
    
    
    /*
     * 七天自动确认收货
     */
    public function cronshouhuo(){
    
        //取出已经发货七天,并且没有收货的订单
        //0待支付,1已付款,,2完成,3.待退款 4已退款,5发货,0未完成,6订单关闭,
        $day = 7;//七天自动确认收货
        $time = date("Y-m-d H:i:s",time()-$day*3600*24);
    
        $where = array(
            "checked" => 5,
            "create_time" => array("lt",$time)
        );
        $orders = $this->orderModel->getOrderLists($where);
        if(empty($orders)){
            exit("no order");
        }
    
        foreach($orders as $k=>$v){
            $this->_shouhuo($v);
        }
    
        exit(count($orders));
    }
    
    

    /*
     * 确认收货
     * 
     */
    protected function _shouhuo($order){
        
         $where = array(
             "order_id" => $order['order_id'],
             "checked" => 5
         );
        
         if($order['checked'] != 5){
             return false;//不能确认收货
         }
        
         //可以确认收货
         $data = array(
             "checked" => 2,
             "shouhuo_time" => date("Y-m-d H:i:s")
         );
         $res = $this->orderModel->editOrder($where, $data);
         if(!$res){
             return false;
         }
        
        /* 确认收货逻辑和佣金解锁逻辑分离
        //判断是否锁定佣金
        $shuoding = C('YONGJIN_LOCK');
        if(!$shuoding){
            //return true;
        }
                
        //佣金有锁定期,解锁
        //查找相关订单
        
        $where_yj = array(
            "order_id" => $order['order_id'],
            "un_lock" => 0,//锁定状态的的相关订单
            "logtype" => 1,//收入
            "mtype" => 1,//佣金
           // "wintype" => array("neq",16),//凝胶的不结算
        );

        $yjlist = $this->moneylogModel->getMoneylogLists($where_yj);

        if(!empty($yjlist)){
            foreach ($yjlist as $k=>$v){
                //扣除锁定佣金,解锁可提佣金
                $money = $v['money'];
                if($money>0){
                    $moneydata = array(
                        "money" => array('exp',"money+".$money),//佣金发放
                        "lock_money" => array('exp',"lock_money-".$money),//锁定佣金
                    );
                    $res2 = $this->updateMemberInfoByUid($v['uid'],$moneydata);
                    if($res2){
                        //改变佣金日志状态
                        $this->moneylogModel->where(array("log_id"=>$v['log_id']))->setField('un_lock',1);
                    }
                }
            }
        }
        
        

        //奖励佣金
        $where_yj = array(
            "order_id" => $order['order_id'],
            "un_lock" => 0,//锁定状态的的相关订单
            "logtype" => 1,//收入
            "mtype" => 12,//佣金
           // "wintype" => array("neq",16),//凝胶的不结算
        );

        $yjlist = $this->moneylogModel->getMoneylogLists($where_yj);

        if(!empty($yjlist)){
            foreach ($yjlist as $k=>$v){
                //扣除锁定佣金,解锁可提佣金
                $money = $v['money'];
                if($money>0){
                    $moneydata = array(
                        "prize_money" => array('exp',"prize_money+".$money),//佣金发放
                        "lockprize_money" => array('exp',"lockprize_money-".$money),//锁定佣金
                    );
                    $res2 = $this->updateMemberInfoByUid($v['uid'],$moneydata);
                    if($res2){
                        //改变佣金日志状态
                        $this->moneylogModel->where(array("log_id"=>$v['log_id']))->setField('un_lock',1);
                    }
                }
            }
        }
        
        */

        return true;
        
        
    }
    
    
    /*
     * 解锁佣金
     */
    protected function _unlock($order){
        $where_yj = array(
            "order_id" => $order['order_id'],
            "un_lock" => 0,//锁定状态的的相关订单
            "logtype" => 1,//收入
            "mtype" => 1,//佣金
            "from_jibie" => array('in',array(1,2))//第三层不解锁
        );
        $yjlist = $this->moneylogModel->getMoneylogLists($where_yj);
        if(!empty($yjlist)){
            foreach ($yjlist as $k=>$v){
                //扣除锁定佣金,解锁可提佣金
                $money = $v['money'];
                if($money>0){
                    $moneydata = array(
                        "money" => array('exp',"money+".$money),//佣金发放
                        "lock_money" => array('exp',"lock_money-".$money),//锁定佣金
                    );
                    $res2 = $this->updateMemberInfoByUid($v['uid'],$moneydata);
                    if($res2){
                        //改变佣金日志状态
                        $this->moneylogModel->where(array("log_id"=>$v['log_id']))->setField('un_lock',1);
                    }
                }
            }
        }
        return true;
    }

    
    /*
     * 每月8号解锁佣金
     * 市级和省级总代每个月自己必须进货50盒/月，否则取消相关奖金！
     */
    public function cronunlock() {
        $day = date('d');
        if($day!=8) {
            return false;
        }
        $start = date('Y-m-00 00:00:00', strtotime('-1 month'));
        $end = date('Y-m-00 00:00:00');
        $orderWhere = $where = array(
            'pay_time' => array('between',array($start,$end)),
            'checked' => array('in',array(1,2,5)),
        );
        
        $orders = $this->orderModel->getOrderLists($where);
        if(empty($orders)){
            exit("no order");
        }
        
        foreach($orders as $k=>$v){
            $this->_unlock($v);
        }
    
        exit(count($orders));
    }
    
    
}